package com.chuangjiangx.member.basic.web.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.util.ClassUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhangquanquan on 2018/7/2.
 */
@Slf4j
public class UniqueNameGenerator extends AnnotationBeanNameGenerator{

    private static Set<String> beanNames = new HashSet<String>();

    @Override
    protected String buildDefaultBeanName(BeanDefinition definition) {

        String className = definition.getBeanClassName();
        //处理bean名称相同注入问题（类名相同）
        if(beanNames.contains(className)){
            return String.format("%s.%s", ClassUtils.getPackageName(definition.getBeanClassName()), ClassUtils.getShortName(definition.getBeanClassName()));
        }
        beanNames.add(className);
        String classNameFinal = StringUtils.substringAfterLast(className, ".");

        String realClassName=classNameFinal.substring(0,1).toLowerCase() + classNameFinal.substring(1);
        log.info("spring bean realName is:"+realClassName);

        return realClassName;
    }

}
